﻿
using Xacml.Core;
using Xacml.Core.Policy;
using Xacml.Core.Runtime;
using System;
using System.Xml;

namespace Xacml.Core.Context
{
  public class ResultElement : XacmlElement
  {
    private ObligationCollection _obligations = new ObligationCollection();
    private string _resourceId;
    private Decision _decision;
    private StatusElement _status;

    public string ResourceId
    {
      get
      {
        return this._resourceId;
      }
    }

    public Decision Decision
    {
      get
      {
        return this._decision;
      }
      set
      {
        this._decision = value;
      }
    }

    public StatusElement Status
    {
      get
      {
        return this._status;
      }
      set
      {
        this._status = value;
      }
    }

    public ObligationCollection Obligations
    {
      get
      {
        return this._obligations;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return true;
      }
    }

    public ResultElement(XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
    }

    public ResultElement(string resourceId, Decision decision, StatusElement status, ObligationCollection obligations, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      this._resourceId = resourceId;
      this._decision = decision;
      this._status = status != null ? status : new StatusElement((StatusCodeElement) null, (string) null, (string) null, schemaVersion);
      if (obligations == null)
        return;
      this._obligations = obligations;
    }

    public ResultElement(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      if (reader.LocalName == "Result")
      {
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "Decision":
              this._decision = (Decision) Enum.Parse(typeof (Decision), reader.ReadElementString(), false);
              break;
            case "Status":
              this._status = new StatusElement(reader, schemaVersion);
              break;
            case "Obligations":
              while (reader.Read())
              {
                switch (reader.LocalName)
                {
                  case "Obligation":
                    this._obligations.Add((ObligationElementReadWrite) new ObligationElement(reader, schemaVersion));
                    break;
                }
                if (reader.LocalName == "Obligations" && reader.NodeType == XmlNodeType.EndElement)
                {
                  reader.Read();
                  break;
                }
              }
              break;
          }
          if (reader.LocalName == "Result" && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }
  }
}
